package net.jatec.flashcards.core.algorithm.supermemo2;

import net.jatec.flashcards.core.Card;


public class SuperMemeo2Impl implements SuperMemo2 {

  public void evaluateCard(Card item, int score) {
    item.setEF(EF(score, item.getEF()));
    item.setInterval(interval(item.getN(), item));
    item.setN(item.getN() + 1);
  }

  public SuperMemeo2Impl() {
  }

  private int interval(int n, Card item) {
    if (n == 1) {
      return 1;
    }
    if (n == 2) {
      return 6;
    }
    int newInterval = (int) (interval(n - 1, item) * item.getEF());
    return newInterval;
  }

  private double EF(int q, double ef) {
    double newEF = ef - 0.8 + 0.28 * q - 0.02 * q * q;
    if (newEF > 2.5) {
      return 2.5;
    } else if (newEF < 1.3) {
      return 1.3;
    }
    return newEF;
  }

}
